/*
 * Filename:    AttendantProviderController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月24日
 */
package com.paic.mhis.hcpms.agreement.healthcareprovider.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.AttendantProviderService;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.ValidatorUtil;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.web.BaseRest;

@Controller
public class AttendantProviderController extends BaseRest {
	@BizServicesProxy
	private AttendantProviderService attendantProviderService;

	private static List<String> insuranceHeaderList = new ArrayList<String>();
	static {
		insuranceHeaderList.add("姓名");
		insuranceHeaderList.add("医疗机构名称");
		insuranceHeaderList.add("医疗机构编号");
		insuranceHeaderList.add("人员分类");
		insuranceHeaderList.add("所属科室");
		insuranceHeaderList.add("手机号码");
		insuranceHeaderList.add("电话号码");
	}

	@RequestMapping(value = "/hcpms/provider/query")
	public Map<String, Object> findHospitalProviderOrgList(
			HttpServletRequest request) {
		Map<String, Object> result = null;
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		try {
			String region = user.getAreaCode();
			String name = request.getParameter("providerName");
			String license = request.getParameter("practiceLicense");
			String orgcode = request.getParameter("providerCode");
			String addressCounty = request.getParameter("addressCounty");
			String orgType = request.getParameter("orgType");
			String orgLevel = request.getParameter("orgLevel");
			String page = request.getParameter("page");
			String pageSize = request.getParameter("pageSize");
			String providertype = request.getParameter("providertype");
			if (!isEmpty(page) || !isEmpty(pageSize)) {
				if (!ValidatorUtil.isPlusInteger(page)
						|| !ValidatorUtil.isPlusInteger(pageSize)) {
					result = new HashMap<String, Object>();
					HttpProcessUtil.generateExceptionResult(result,
							Constants.STATE_KEY,
							Constants.FAILED_VALIDATE_STATE_VALUE,
							Constants.MSG_KEY,
							Constants.PAGE_VALIDATE_FAILED_MSG);
					return result;
				}
			}
			JSONObject transferJson = new JSONObject();
			transferJson.put("providerName", name);
			transferJson.put("practiceLicense", license);
			transferJson.put("providerCode", orgcode);
			transferJson.put("addressCounty", addressCounty);
			transferJson.put("orgType", orgType);
			transferJson.put("orgLevel", orgLevel);
			transferJson.put("page", page);
			transferJson.put("pageSize", pageSize);
			transferJson.put("providertype", providertype);
			transferJson.put("regionCode", region);
			transferJson.put("userName", user.getUsername());
			result = attendantProviderService
					.findHospitalProviderOrgList(transferJson);
			result.put("authority", user.getRegionAuthority());
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	@RequestMapping(value = "/hcpms/provider/queryDetail.do", method = RequestMethod.GET)
	public Map<String, Object> findHospitalProviderOrgDetail(
			HttpServletRequest request) {
		Map<String, Object> result = null;
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		try {
			JSONObject transferJson = new JSONObject();
			String providerId = request.getParameter("providerid");
			String type = request.getParameter("type");
			transferJson.put("providerId", providerId);
			transferJson.put("type", type);
			result = attendantProviderService
					.findHospitalProviderOrgDetail(transferJson);
			result.put("authority", user.getRegionAuthority());
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	@RequestMapping(value = "/hcpms/provider/getProviderCode.do", method = RequestMethod.GET)
	public Map<String, Object> getProviderCode(HttpServletRequest request) {
		Map<String, Object> result = null;
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		try {
			String region = user.getAreaCode();
			JSONObject transferJson = new JSONObject();
			transferJson.put("region", region);
			result = attendantProviderService.getProviderCode(transferJson);
			result.put("authority", user.getRegionAuthority());
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	@RequestMapping(value = "/hcpms/provider/delete.do", method = RequestMethod.POST)
	public Map<String, Object> deleteHospitalProviderOrg(
			HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");

		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			transferJson.put("updatedBy", user.getUsername());
			result = attendantProviderService
					.deleteHospitalProviderOrg(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	@RequestMapping(value = "/hcpms/provider/save.do", method = RequestMethod.POST)
	public Map<String, Object> saveHospitalProviderOrg(
			HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			transferJson.put("createBy", user.getUsername());
			transferJson.put("updateBy", user.getUsername());
			transferJson.put("regionCode", user.getAreaCode());
			result = attendantProviderService
					.saveHospitalProviderOrg(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	private boolean isEmpty(String param) {
		return param == null || param.equals("");
	}

	/**
	 * 查询医保服务人员列表信息
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/hcpms/provider/findAttendantInfoList")
	public Map<String, Object> findAttendantInfoList(HttpServletRequest request) {
		Map<String, Object> result = null;
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		try {
			String healthProviderName = request
					.getParameter("healthProviderName");
			String hospitalLevel = request.getParameter("hospitalLevel");
			String managementCode = request.getParameter("managementCode");
			String personType = request.getParameter("personType");
			String qualifiedLevel = request.getParameter("qualifiedLevel");
			String certificateNo = request.getParameter("certificateNo");
			String name = request.getParameter("name");
			String departNo = request.getParameter("departNo");
			String practiceLicense = request.getParameter("practiceLicense");
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("healthProviderName", healthProviderName);
			map.put("hospitalLevel", hospitalLevel);
			map.put("managementCode", managementCode);
			map.put("personType", personType);
			map.put("qualifiedLevel", qualifiedLevel);
			map.put("certificateNo", certificateNo);
			map.put("name", name);
			map.put("departNo", departNo);
			map.put("practiceLicense", practiceLicense);
			map.put("userName", user.getUsername());
			result = attendantProviderService.findAttendantInfoList(page,
					pageSize, map);
			result.put("authority", user.getRegionAuthority());
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 查询医保服务人员详情信息
	 * 
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value = "/hcpms/attendant/findAttendantDetail.do", method = RequestMethod.POST)
	public Map<String, Object> findAttendantDetail(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String certificateNo = request.getParameter("certificateNo");
			String insuranceId = request.getParameter("insuranceId");
			JSONObject transferJson = new JSONObject();
			transferJson.put("certificateNo", certificateNo);
			transferJson.put("insuranceId", insuranceId);
			result = attendantProviderService.findAttendantDetail(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 变更记录列表信息 paramDTO.changeType 0:医院;1:医院服务人员
	 * 
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value = "/hcpms/attendant/findHistoryChangeRecord")
	public Map<String, Object> findHistoryChangeRecord(
			HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String relationId = request.getParameter("relationId");
			String changeType = request.getParameter("changeType");
			JSONObject transferJson = new JSONObject();
			transferJson.put("relationId", relationId);
			transferJson.put("changeType", changeType);
			result = attendantProviderService
					.findHistoryChangeRecord(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据条件获取医保服务人员总数据条数
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcpms/provider/findAttendantInfoCount")
	@ResponseBody
	public Map<String, Object> exportRulesCount(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			String healthProviderName = request
					.getParameter("healthProviderName");
			String hospitalLevel = request.getParameter("hospitalLevel");
			String managementCode = request.getParameter("managementCode");
			String personType = request.getParameter("personType");
			String qualifiedLevel = request.getParameter("qualifiedLevel");
			String certificateNo = request.getParameter("certificateNo");
			String name = request.getParameter("name");
			String practiceLicense = request.getParameter("practiceLicense");

			params.put("healthProviderName", healthProviderName);
			params.put("hospitalLevel", hospitalLevel);
			params.put("managementCode", managementCode);
			params.put("personType", personType);
			params.put("qualifiedLevel", qualifiedLevel);
			params.put("certificateNo", certificateNo);
			params.put("name", name);
			params.put("practiceLicense", practiceLicense);
			int total = this.attendantProviderService.queryContactCount(params);
			result.put("total", total);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 查询医保服务人员列表信息
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/hcpms/provider/changeRecord")
	public Map<String, Object> findChangeRecord(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String providerid = request.getParameter("providerid");
			result = attendantProviderService.findChangeRecord(providerid);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
}
